package com.market.plus.controller;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.market.plus.controller.mapper.JsonMapping;
import com.market.plus.controller.validator.RelationshipValidator;
import com.market.plus.core.dto.ParticipantDto;
import com.market.plus.core.dto.RelationshipDto;
import com.market.plus.core.service.ParticipantService;

@Controller
public class ConfigurationController {

   public static final String CONFIG_PAGE = "participant/configuration/config";
   public static final String WAITING_PAGE = "participant/configuration/waiting";
   public static final String MYFRIENDS_PAGE = "participant/configuration/myfriends";
   public static final String MYLOCKDEDFRIENDS_PAGE = "participant/configuration/lockedfriends";
   public static final String STATUS_PAGE = "participant/configuration/status";
   
   public static final String RELATIONSHIP_PAGE = "participant/configuration/relationship";
   public static final String RELATIONSHIP_ADD_PAGE = "participant/configuration/relationship/add";
   public static final String RELATIONSHIP_STATUS_PAGE = "participant/configuration/relationship/status";

   private ParticipantService participantService;
   private RelationshipValidator relationshipValidator;
   private JsonMapping jsonMapping;

   @Autowired
   public ConfigurationController(
      @Qualifier("participantService") ParticipantService participantService,
      @Qualifier("relationshipValidator") RelationshipValidator relationshipValidator,
      @Qualifier("jsonMapping") JsonMapping jsonMapping) {
      this.participantService = participantService;
      this.relationshipValidator = relationshipValidator;
      this.jsonMapping = jsonMapping;
   }

   @RequestMapping(value = "/participant/configuration", method = RequestMethod.GET)
   public String viewConfiguration(ModelMap model) {
      return CONFIG_PAGE;
   }

   @RequestMapping(value = "/participant/addFriend", method = RequestMethod.POST)
   public @ResponseBody JSONObject viewConfiguration(@RequestParam("friendId")Long friendId, ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      this.participantService.requestMakingFriend(participant.getId(), friendId);
      JSONObject jsonObject = this.jsonMapping.requestFriendStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/waitingAccept", method = RequestMethod.GET)
   public String viewWaiting(ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      List<ParticipantDto> participantDtos = this.participantService.waitingFriends(participant.getId());
      model.put("waitingFriends", participantDtos);
      return WAITING_PAGE;
   }

   @RequestMapping(value = "/participant/friend/list", method = RequestMethod.GET)
   public String viewMyFriends(ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      List<ParticipantDto> participantDtos = this.participantService.listMyFriends(participant.getId());
      model.put("myFriends", participantDtos);
      return MYFRIENDS_PAGE;
   }

   @RequestMapping(value = "/participant/friend/lockedList", method = RequestMethod.GET)
   public String viewMyLockedFriends(ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      List<ParticipantDto> participantDtos = this.participantService.listMyLockedFriends(participant.getId());
      model.put("myLockedFriends", participantDtos);
      return MYLOCKDEDFRIENDS_PAGE;
   }

   @RequestMapping(value = "/participant/relationship/list", method = RequestMethod.GET)
   public @ResponseBody JSONObject viewRelationships(ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      List<ParticipantDto> participantDtos = this.participantService.listMyFriends(participant.getId());
      List<RelationshipDto> relationships = this.participantService.listMyRelationship(participant.getId());
      JSONObject jsonObject = this.jsonMapping.convertRelationship(participantDtos, relationships);
      return jsonObject;
   }

   @RequestMapping(value = "/participant/relationship/add", method = RequestMethod.GET)
   public String viewRelationshipAddForm(ModelMap model) {
      return RELATIONSHIP_ADD_PAGE;
   }

   @RequestMapping(value = "/participant/relationship/add", method = RequestMethod.POST)
   public @ResponseBody JSONObject addNewRelationship(@ModelAttribute("relationship")RelationshipDto relationshipDto, BindingResult result, ModelMap model) throws Exception {
      ParticipantDto participantDto = this.participantService.findLoginUserName(true);
      this.relationshipValidator.validateRelationship(relationshipDto, result, participantDto.getId());
      if (!result.hasErrors()){
         Long id=this.participantService.addNewRelationship(participantDto.getId(), relationshipDto);
         relationshipDto.setId(id);
      }
      JSONObject jsonObject = this.jsonMapping.addRelationshipStatus(relationshipDto, result);
      return jsonObject;
   }
   
   @RequestMapping(value = "/participant/relationship/remove", method = RequestMethod.POST)
   public @ResponseBody JSONObject addNewRelationship(@RequestParam("relationshipId")long relationshipId, ModelMap model) throws Exception {
      ParticipantDto participantDto = this.participantService.findLoginUserName(true);
      this.participantService.removeRelationship(participantDto.getId(), relationshipId);
      JSONObject jsonObject = this.jsonMapping.removeRelationshipStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/relationship/add/friend", method = RequestMethod.POST)
   public @ResponseBody JSONObject addFriendToRelationship(@RequestParam("relationshipId")long relationshipId,@RequestParam("friendId")long friendId,ModelMap model) throws Exception {
      ParticipantDto participantDto = this.participantService.findLoginUserName(true);
      this.participantService.addFriendToRelationship(participantDto.getId(), relationshipId, friendId);
      JSONObject jsonObject = this.jsonMapping.addFriendToRelationshipStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/relationship/remove/friend", method = RequestMethod.POST)
   public @ResponseBody JSONObject removeFriendToRelationship(@RequestParam("relationshipId")long relationshipId,@RequestParam("friendId")long friendId,ModelMap model) throws Exception {
      ParticipantDto participantDto = this.participantService.findLoginUserName(true);
      this.participantService.removeFriendFromRelationship(participantDto.getId(), relationshipId, friendId);
      JSONObject jsonObject = this.jsonMapping.removeFriendFromRelationshipStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/accept/{friendId}", method = RequestMethod.POST)
   public @ResponseBody JSONObject accepted(@PathVariable("friendId")Long friendId, ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      this.participantService.acceptFriend(participant.getId(), friendId);
      JSONObject jsonObject = this.jsonMapping.acceptFriendStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/deny/{friendId}", method = RequestMethod.POST)
   public @ResponseBody JSONObject deny(@PathVariable("friendId")Long friendId, ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      this.participantService.denyFriends(participant.getId(), new long[]{friendId});
      JSONObject jsonObject = this.jsonMapping.denyFriendStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/lock/{friendId}", method = RequestMethod.POST)
   public @ResponseBody JSONObject lock(@PathVariable("friendId")Long friendId, ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      this.participantService.lockFriend(participant.getId(), friendId);
      JSONObject jsonObject = this.jsonMapping.lockedFriendStatus();
      return jsonObject;
   }

   @RequestMapping(value = "/participant/unlock/{friendId}", method = RequestMethod.POST)
   public @ResponseBody JSONObject unlock(@PathVariable("friendId")Long friendId, ModelMap model) {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      this.participantService.unlockFriend(participant.getId(), friendId);
      JSONObject jsonObject = this.jsonMapping.unlockedFriendStatus();
      return jsonObject;
   }
}
